package org.penguin.study.bk.base.api

import io.swagger.v3.oas.annotations.media.Schema
import org.penguin.study.bk.constant.HttpStatus
import java.io.Serializable

@Schema(title = "接口返回对象")
class Return<T> : Serializable {
    /**
     * 成功标志
     */
    @Schema(title = "成功标志")
    var success = true

    /**
     * 返回处理消息
     */
    @Schema(title = "返回处理消息")
    var message = ""

    /**
     * 返回代码
     */
    @Schema(title = "返回代码")
    var code = 0

    /**
     * 返回数据对象 data
     */
    @Schema(title = "返回数据对象")
    var result: T? = null

    /**
     * 时间戳
     */
    @Schema(title = "时间戳")
    val timestamp = System.currentTimeMillis()

    private constructor()

    /**
     * 兼容VUE3版token失效不跳转登录页面
     *
     * @param code
     * @param message
     */
    constructor(code: Int, message: String) {
        this.code = code
        this.message = message
    }

    fun success(message: String): Return<T> {
        this.message = message
        this.code = HttpStatus.OK
        success = true
        return this
    }

    fun error500(message: String): Return<T> {
        this.message = message
        this.code = HttpStatus.Internal_Server_Error
        success = false
        return this
    }

    companion object {
        private const val serialVersionUID = -6841994746990357693L
        fun <T> ok(): Return<T> {
            val r: Return<T> = Return<T>()
            r.success = true
            r.code = HttpStatus.OK
            return r
        }

        fun <T> ok(msg: String): Return<T> {
            val r: Return<T> = Return<T>()
            r.success = true
            r.code =HttpStatus.OK
            r.message = msg
            return r
        }

        fun <T> ok(data: T): Return<T> {
            val r: Return<T> = Return<T>()
            r.success = true
            r.message = "操作成功"
            r.code = HttpStatus.OK
            r.result = data
            return r
        }

        fun <T> ok(msg: String, data: T): Return<T> {
            val r: Return<T> = Return<T>()
            r.success = true
            r.code = HttpStatus.OK
            r.message = msg
            r.result = data
            return r
        }

        fun <T> error(msg: String, data: T): Return<T> {
            val r: Return<T> = Return()
            r.success = true
            r.code = HttpStatus.Internal_Server_Error
            r.message = msg
            r.result = data
            return r
        }

        fun error(msg: String): Return<Any> {
            return error(HttpStatus.Internal_Server_Error, msg)
        }

        fun error(code: Int, msg: String): Return<Any> {
            val r = Return<Any>()
            r.code = code
            r.message = msg
            r.success = true
            return r
        }

        /**
         * 无权限访问返回结果
         */
        fun noAuth(msg: String): Return<Any> {
            return error(HttpStatus.Not_Extended, msg)
        }
    }
}